package org.battery.controller;

import org.battery.entity.Battery;
import org.battery.entity.Page;
import org.battery.service.BatteryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 1t on 2017-08-16.
 */
@Controller
public class BatteryController extends BaseController{

    @Resource(name = "batteryService")
    public BatteryService batteryService;

    @RequestMapping(value = "/searchBatteryTime")
    @ResponseBody
    public Map<String, Object> searchBatteryTime() {
        Map<String, Object> map = new HashMap<String, Object>();
        Battery battery = new Battery();

        List<Battery> batteryTime = null;
        try {
            batteryTime = batteryService.getBatteryTime();
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("batteryTimeList", batteryTime);
        battery = null;
        return map;
    }

    @RequestMapping(value = "/searchBatteryDay")
    @ResponseBody
    public Map<String, Object> searchBatteryDay( Battery battery) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Battery> batteryDay = null;
        try {
            batteryDay = batteryService.getBatteryDay(battery);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("batteryDayList", batteryDay);
        battery = null;
        return map;
    }

    @RequestMapping(value = "/searchBatteryName")
    @ResponseBody
    public Map<String, Object> searchBatteryName(Battery battery) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Battery> batteryName = null;
        try {
            batteryName = batteryService.getBatteryName(battery);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("batteryNameList", batteryName);
        battery = null;
        return map;
    }

    @RequestMapping(value = "/searchBatteryAll")
    @ResponseBody
    public Map<String, Object> searchBatteryAll(Battery battery, String page, String rows) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page pageQuery = getPage();
        pageQuery.setCurrentPage(Integer.parseInt(page));
        pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
        pageQuery.setShowCount(Integer.parseInt(rows));

        List<Battery> batteryListAll = null;
        try {
            batteryListAll = batteryService.getBattery(battery, pageQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        map.put("totalCnt", pageQuery.getTotalResult());
        map.put("totalPage", pageQuery.getTotalPage());
        map.put("batteryListAll", batteryListAll);
        battery = null;
        return map;
    }

    @RequestMapping(value = "/saveBattery")
    @ResponseBody
    public Map<String, Object> saveDriverInfo(Battery battery) {

        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            count = batteryService.insertBattery(battery);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", "Error");
        }
        battery = null;
        return map;
    }

    @RequestMapping(value = "/updateBattery")
    @ResponseBody
    public Map<String, Object> updateDriverInfo(Battery battery) {

        Map<String, Object> map = new HashMap<String, Object>();
        int count = 0;
        try {
            count = batteryService.updateBattery(battery);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", "Error");
        }
        battery = null;
        return map;
    }

    @RequestMapping(value = "/deleteBattery")
    @ResponseBody
    public Map<String, Object> deleteBattery(String BatteryIDList) {

        HashMap<String, Object> map = new HashMap<String, Object>();
        String[] BatteryID = BatteryIDList.split(",");
        Battery battery = new Battery();
        battery.setBatteryIDList(BatteryID);

        int count = 0;
        try {
            count = batteryService.deleteBattery(battery);
            if (count > 0) {
                map.put("result", "Success");
            } else {
                map.put("result", "NotFound");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result", "Error");
            return map;
        }
        battery = null;
        return map;
    }
}
